<?php

$__FILEPATH__ = dirname(__FILE__)."/";
require_once($__FILEPATH__."../func_common.php");
require_once($__FILEPATH__."../class_def.php");
require_once($__FILEPATH__."../conf/info.php");

CheckAdminAndInitHeader();

$PageArray = array();
$mysqli = GetMysqliInstance();


if (isset($_POST["add"]))
{
	$ret = false;
	if(isset($_POST["cname"]) && $_POST["cname"] != ""
		&& isset($_POST["cdate"]) && $_POST["cdate"] != ""
		&& isset($_POST["teamname"]) && $_POST["teamname"] != ""
		&& isset($_POST["teammember"]) && $_POST["teammember"] != "")
	{
		$cname = $_POST["cname"];
		$cdate = $_POST["cdate"];
		$teamname = $_POST["teamname"];
		$teammember = $_POST["teammember"];
		$award = $_POST["award"];
		$coach = $_POST["coach"];
		$extra = $_POST["extra"];
		
		$sqlstr = "insert into recordlist (cname,cdate,teamname,teammember,award,coach,extra) values (?, ?, ?, ?, ?, ?, ?)";
		$stmt = $mysqli->prepare($sqlstr);
		$stmt->bind_param("sssssss", $cname, $cdate, $teamname, $teammember, $award, $coach, $extra);
		$ret = $stmt->execute();
		$stmt->close();
	}		
	
	if ($ret)
	{
		echo "<script type='text/javascript'>alert('Congratulations,添加成功！');</script>";
	}
	else 
	{
		echo "<script type='text/javascript'>alert('*地方填写不能为空或数据库连接错误！');</script>";
		exit(0);
	}
}
else if(isset($_POST["delete"]))
{
	$id = intval($_POST["id"]);
	if (!$mysqli->query("delete from recordlist where id = $id"))
	{
		echo "<script type='text/javascript'>alert('删除失败！');</script>";
		exit(0);
	}
}
else if(isset($_POST["update"]))
{
	$ret = false;
	if(isset($_POST["cname"]) && $_POST["cname"] != ""
		&& isset($_POST["cdate"]) && $_POST["cdate"] != ""
		&& isset($_POST["teamname"]) && $_POST["teamname"] != ""
		&& isset($_POST["teammember"]) && $_POST["teammember"] != "")
	{
		$id = intval($_POST["id"]);
		$cname = $_POST["cname"];
		$cdate = $_POST["cdate"];
		$teamname = $_POST["teamname"];
		$teammember = $_POST["teammember"];
		$award = $_POST["award"];
		$coach = $_POST["coach"];
		$extra = $_POST["extra"];

		if ($award == "")
			$award = "-";
		if ($coach == "")
			$coach = "-";
		if ($extra == "")
			$extra = "-";

		$sqlstr="update recordlist set cname=?, cdate=?, teamname=?, teammember=?, award=?, coach=?, extra=? where id=$id";
		$stmt = $mysqli->prepare($sqlstr);
		$stmt->bind_param("sssssss", $cname, $cdate, $teamname, $teammember, $award, $coach, $extra);
		$ret = $stmt->execute();
		$stmt->close();
	}

	if ($ret == false)
	{
		echo "<script type='text/javascript'>alert('*填写不能为空或数据库连接错误！');</script>";
		exit(0);
	}
}


// 查询

$year = intval(substr(GetSysTime(), 0, 4)) - 1;
if (isset($_POST["year"]))
	$year = intval($_POST["year"]);

$PageArray["yearlist"] = array();

$start = 2008;
$max_y = intval(date("Y"));
while($start <= $max_y){
	$PageArray["yearlist"][] = $start;
	++$start;
}

$PageArray["recordlist"] = array();
$result = $mysqli->query("select * from recordlist where cdate like \"%$year%\" order by cdate desc", MYSQLI_USE_RESULT);
if ($result)
{
	while($row = $result->fetch_array(MYSQLI_ASSOC))
	{
		$RecordItem = new RecordListItem();
		$RecordItem->id 		= $row["id"];
		$RecordItem->cname 		= $row["cname"];
		$RecordItem->cdate 		= $row["cdate"];
		$RecordItem->teamname 	= $row["teamname"];
		$RecordItem->teammember = $row["teammember"];
		$RecordItem->coach 		= $row["coach"];
		$RecordItem->award 		= $row["award"];
		$RecordItem->extra 		= $row["extra"];
		$PageArray["recordlist"][] = $RecordItem;
	}
	$result->close();
}

$mysqli->close();

?>